package com.host.tab;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.host.tab.chart.AverageTemperatureChart;
import com.host.tab.chart.BudgetPieChart;
import com.host.tab.chart.IDemoChart;
import com.host.tab.chart.SalesStackedBarChart;

public class VisualizeDisplayActivity extends ListActivity{

	private IDemoChart[] mCharts = new IDemoChart[] {
			new AverageTemperatureChart(), new BudgetPieChart(),
			new SalesStackedBarChart() };	
	private String TAG = "visualize_data";
	
	private ContentResolver resolver = null;
	
	private String mListText[] ;
	private String mListSummary[];
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.visualize);
		
		int length = mCharts.length;
		mListText = new String[length];
		mListSummary = new String[length];
		
		for (int i = 0; i < length; i++){
			mListText[i] = mCharts[i].getName();
			mListSummary[i] = mCharts[i].getDesc();	
		}
		
		SimpleAdapter Adapter = new SimpleAdapter(this, getListValues(),
				android.R.layout.simple_list_item_2, new String[] {
						IDemoChart.NAME, IDemoChart.DESC }, new int[] {
						android.R.id.text1, android.R.id.text2 });
		
		setListAdapter(Adapter);
		
	}

	
	private List<Map<String,String>> getListValues(){
		List<Map<String,String>> values = new ArrayList<Map<String,String>>();
		int length = mListText.length;
		for (int i = 0; i<length ; i++){
			Map<String,String> v = new HashMap<String, String>();
			v.put(IDemoChart.NAME, mListText[i]);
			v.put(IDemoChart.DESC, mListSummary[i]);
			values.add(v);
		}
		
		return values;
	}


	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		if (position == 0){
			LineChartMonth();
		}
		else if (position == 1){
			PieChartAll();
		}
		else if (position == 2){
			BarChartMonth();
		}
		
	}
	
	//****************************** 绘图函数*******************************
	
	public void LineChartMonth(){    //折线图
		AverageTemperatureChart mChart = new AverageTemperatureChart();
		Intent intent = null;
		
		String projection[] = {"date"};
		String selection = "1=1";
		
		Date date = null;
		//用来取出数据库内的时间值，为string类型
		String temp_date_string = null;
		//临时存储返回的日期
		int temp_day = 1;
		
		//用来存储每一天的通话次数
		int month_day_times_int[] = new int[32];
		double month_day_times_double[] = new double[32];
		
		
		int i =0;
		for (i = 0; i< 32; i++){
			month_day_times_int[i] = 0;
			month_day_times_double[i] = 0;
		}
		
		//select date from calls where 1=1
		Cursor cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, projection, selection, null, null);
		startManagingCursor(cursor);
		cursor.moveToFirst();

		while (!cursor.isAfterLast()){
			temp_date_string = cursor.getString(cursor.getColumnIndex("date"));
			date = new Date(Long.parseLong(temp_date_string));
			
			//getYear()返回的是自从1900年开始至今为止多少年。
			//Log.i(TAG, "现在是"+date.getMonth()+"");
			//getMonth()返回值是  0~11，对应1~12月！！！！
			if (date.getMonth() == 4){          //是4月份
				//Log.i(TAG, "进入5月份中！@#！@#！@#");
				temp_day = date.getDate();
				//Log.i(TAG, "当前是第"+temp_day+"天");
				(month_day_times_int[--temp_day])++;
				month_day_times_double[temp_day] = new Double(month_day_times_int[temp_day]);
				Log.i(TAG, Double.toString(month_day_times_double[temp_day]));
			}
			
			cursor.moveToNext();
		}
		
		
		List<double[]> list_values = new ArrayList<double[]>();
		list_values.add(month_day_times_double);
		mChart.data_fill(list_values);
		
		intent = mChart.execute(this);
		startActivity(intent);
		
		/*
		SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Date date_converted = new Date(Long.parseLong(temp_date));

		// date为转换后的日期字符串
		String date = sfd.format(date_converted);
		*/
	}
	
	//柱状图
	public void BarChartMonth(){
		SalesStackedBarChart mChart = new SalesStackedBarChart();
		
		Intent intent = null;
		String projection[] = {"date","duration"};
		String selection = "1=1";
		
		Date date = null;
		//用来取出数据库内的时间值，通话时长值，为string类型
		String temp_date_string = null;
		String temp_duration_string = null;
		double temp_duration_double = 0;
		//临时存储返回的日期
		int temp_day = 1;
		
		//用来存储每一天的通话次数
		//int month_day_times_int[] = new int[32];
		double month_day_duration_double[] = new double[32];
		
		
		int i =0;
		for (i = 0; i< 32; i++){
			//month_day_times_int[i] = 0;
			month_day_duration_double[i] = 0;
		}
		
		//select date from calls where 1=1
		resolver = getContentResolver();
		Cursor cursor = resolver.query(CallLog.Calls.CONTENT_URI, projection, selection, null, null);
		startManagingCursor(cursor);
		cursor.moveToFirst();

		while (!cursor.isAfterLast()){
			temp_date_string = cursor.getString(cursor.getColumnIndex("date"));
			temp_duration_string = cursor.getString(cursor.getColumnIndex("duration"));
			temp_duration_double = Double.valueOf(temp_duration_string);
			date = new Date(Long.parseLong(temp_date_string));
			
			//getYear()返回的是自从1900年开始至今为止多少年。
			Log.i(TAG, "现在是"+date.getMonth()+"");
			//getMonth()返回值是  0~11，对应1~12月！！！！
			if (date.getMonth() == 4){          //是3月份
				//Log.i(TAG, "进入5月份中！@#！@#！@#");
				temp_day = date.getDate();
				//Log.i(TAG, "当前是第"+temp_day+"天");
				month_day_duration_double[--temp_day] = month_day_duration_double[temp_day]+temp_duration_double;
				//month_day_duration_double[temp_day] = new Double(month_day_times_int[temp_day]);
				//Log.i(TAG, Double.toString(month_day_duration_double[temp_day]));
			}
			
			cursor.moveToNext();
		}
		
		/*
		for (i = 0; i< 32; i++){
			month_day_duration_double[i] = month_day_duration_double[i]/60;
		}
		*/
		
		List<double[]> list_values = new ArrayList<double[]>();
		list_values.add(month_day_duration_double);
		mChart.data_fill(list_values);
		
		intent = mChart.execute(this);
		startActivity(intent);
		
		
	}
	

	
	public void PieChartAll(){   // 全部通话类型绘图   （饼状图）
		BudgetPieChart mChart = new BudgetPieChart();
		
		String projection[] = {"type","count(*) as times"};
		String selection = "1=1) group by (type";
		
		double times[] = new double[3];
		int i = 0;
		Intent intent = null;
		
		resolver = getContentResolver();
		//SELECT type, count(*) as times FROM calls WHERE (1=1) group by (type) ORDER BY null
		Cursor cursor = resolver.query(CallLog.Calls.CONTENT_URI, projection, selection, null, null);
		startManagingCursor(cursor);
		
		for (cursor.moveToFirst(), i=0; i<3 ; cursor.moveToNext(),i++){
			Log.i(TAG, "执行完了moveToFirst!!");
			times[i] =  Double.valueOf(cursor.getString(cursor.getColumnIndex("times")));
			//Log.i(TAG, Double.toString(times[i]));   打印从数据库取出的电话次数
		}
		
		mChart.data_fill(times);
		intent = mChart.execute(this);
		startActivity(intent);
		
	}
	

	
}
